package com.bz.xtcx.manager.mapper.provider;

import com.bz.xtcx.manager.entity.CmsSharePlatform;
import com.bz.xtcx.manager.persist.conditions.SharePlatformCondition;
import com.bz.xtcx.manager.utils.WzStrUtil;
import org.apache.ibatis.jdbc.SQL;

public class CmsSharePlatformProvider {

    public String updateByPrimaryKeySelectiveProvider(CmsSharePlatform csp) {
        return new SQL() {
            {
                UPDATE("cms_share_platform");
                if (null != csp.getSpCompany()) {
                    SET("sp_company = #{spCompany, jdbcType=VARCHAR}");
                }
                if (null != csp.getSpDomain()) {
                    SET("sp_domain = #{spDomain, jdbcType=VARCHAR}");
                }
                if (null != csp.getSpLevel()) {
                    SET("sp_level = #{spLevel, jdbcType=VARCHAR}");
                }
                if (null != csp.getSpLink()) {
                    SET("sp_link = #{spLink, jdbcType=VARCHAR}");
                }
                if (null != csp.getSpName()) {
                    SET("sp_name = #{spName, jdbcType=VARCHAR}");
                }
                WHERE("id = #{id, jdbcType=INTEGER}");
            }
        }.toString();
    }

    public String selectByConditionProvider(SharePlatformCondition spc) {
        StringBuilder sql = new StringBuilder("select * from `cms_share_platform` where 1 = 1 ");
        if (WzStrUtil.isNotBlank(spc.getDomain())) {
            sql.append(" and sp_domain like '%" + spc.getDomain() + "%' ");
        }
        if (WzStrUtil.isNotBlank(spc.getLevel())) {
            sql.append(" and sp_level like '%" + spc.getLevel() + "%' ");
        }
        if (WzStrUtil.isNotBlank(spc.getKeyStr())) {
            sql.append(" and (sp_name like '%" + spc.getKeyStr() + "%' ");
            sql.append("or sp_company like '%" + spc.getKeyStr() + "%' ");
            sql.append("or sp_domain like '%" + spc.getKeyStr() + "%' ");
            sql.append("or sp_level like '%" + spc.getKeyStr() + "%' ");
            sql.append("or sp_link like '%" + spc.getKeyStr() + "%') ");
        }
        sql.append(" order by id ");
        return sql.toString();
    }

    public String countByConditionProvider(SharePlatformCondition spc) {
        StringBuilder sql = new StringBuilder("select count(id) from `cms_share_platform` where 1 = 1 ");
        if (WzStrUtil.isNotBlank(spc.getDomain())) {
            sql.append(" and sp_domain like '%" + spc.getDomain() + "%' ");
        }
        if (WzStrUtil.isNotBlank(spc.getLevel())) {
            sql.append(" and sp_level like '%" + spc.getLevel() + "%' ");
        }
        if (WzStrUtil.isNotBlank(spc.getKeyStr())) {
            sql.append(" and (sp_name like '%" + spc.getKeyStr() + "%' ");
            sql.append("or sp_company like '%" + spc.getKeyStr() + "%' ");
            sql.append("or sp_domain like '%" + spc.getKeyStr() + "%' ");
            sql.append("or sp_level like '%" + spc.getKeyStr() + "%' ");
            sql.append("or sp_link like '%" + spc.getKeyStr() + "%') ");
        }
        return sql.toString();
    }
}
